import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Observable';

import {Config} from '../configs/config';
import {PaginationItem} from '../models/pagination.item';
import {SurveySummary} from '../models/survey.summary';
import { SourceNode } from 'source-map';

const httpOptions = {
  headers: new HttpHeaders({'Content-Type': 'application/json'})
};

@Injectable()
export class SurveyService {
  constructor(private http: HttpClient) {}

  getPopularSurveys(): Observable<PaginationItem<SurveySummary>> {
    return this.http.get<PaginationItem<SurveySummary>>(
        Config.REST_BASE_URL + '/survey/popular.json', httpOptions);
  }

  getLatestSurveys(): Observable<PaginationItem<SurveySummary>> {
    return this.http.get<PaginationItem<SurveySummary>>(
        Config.REST_BASE_URL + '/survey/latest.json', httpOptions);
  }

  getSearchSurveys(keyword: string, page: number, sortBy: string, direction: string): Observable<PaginationItem<SurveySummary>> {
    let params = new HttpParams().set('keyword', keyword).set('page', page + '').set('sort', sortBy + ',' + direction);
    let options = Object.assign({}, httpOptions, {params: params});
    return this.http.get<PaginationItem<SurveySummary>>(
        Config.REST_BASE_URL + '/survey/list.json', options);
  }
}